package com.bill.gaty.mfidelity.dao.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.bill.gaty.mfidelity.beans.Compatibilite;
import com.bill.gaty.mfidelity.dao.CompatibiliteDao;
import com.bill.gaty.mfidelity.util.DaoAbstract;
import com.bill.gaty.mfidelity.util.GtfConstantes;
 
 
public class CompatibiliteDaoImpl extends DaoAbstract implements CompatibiliteDao {

	public CompatibiliteDaoImpl(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		 
	}

	protected List<Compatibilite> cursorToCompatibilites(Cursor c) {
		// TODO Auto-generated method stub
		
		if (c==null||c.getCount() == 0)
			return null;
 List<Compatibilite> compatibilites=new ArrayList<Compatibilite>();
		  
 Compatibilite compatibilite = null;
		 while(c.moveToNext()){
			 
			 compatibilite = new   Compatibilite();
			 compatibilite.setDate(new Date(c.getLong(GtfConstantes.NUM_COL_COMPATIBILITE_DATE)));
			 compatibilite.setElle(c.getString(GtfConstantes.NUM_COL_COMPATIBILITE_ELLE));
			 compatibilite.setLui(c.getString(GtfConstantes.NUM_COL_COMPATIBILITE_LUI));
			 compatibilite.setId((c.getLong(GtfConstantes.NUM_COL_COMPATIBILITE_ID)));
			 compatibilite.setResult(c.getString(GtfConstantes.NUM_COL_COMPATIBILITE_RESULT));
			 compatibilites.add(compatibilite);
			 
		 }
		c.close();
 
		 
		return compatibilites;
	}

	@Override
	public long add(Compatibilite compatibilite) {
		Compatibilite com=findByLuiEtElle(compatibilite.getLui(),compatibilite.getElle());
		if(null!=com){
			compatibilite.setId(com.getId()); 
			  update(compatibilite);
			  return com.getId();
			
		}
		ContentValues values = new ContentValues();
	 	values.put(GtfConstantes.COL_COMPATIBILITE_LUI,compatibilite.getLui());
	 	values.put(GtfConstantes.COL_COMPATIBILITE_ELLE,compatibilite.getElle());
	 	values.put(GtfConstantes.COL_COMPATIBILITE_RESULT,compatibilite.getResult());
	 	values.put(GtfConstantes.COL_COMPATIBILITE_DATE,compatibilite.getDate().getTime()+"");
	 	  
	 
	
	 long id= getBdd().insert(GtfConstantes.TABLE_COMPATIBILITE, null, values);
	 compatibilite.setId(id);
	 return id;
	}

	@Override
	public long update(Compatibilite compatibilite) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
	 	values.put(GtfConstantes.COL_COMPATIBILITE_LUI,compatibilite.getLui());
	 	values.put(GtfConstantes.COL_COMPATIBILITE_ELLE,compatibilite.getElle());
	 	values.put(GtfConstantes.COL_COMPATIBILITE_RESULT,compatibilite.getResult());
	 	values.put(GtfConstantes.COL_COMPATIBILITE_DATE,compatibilite.getDate().getTime()+"");
	 	values.put(GtfConstantes.COL_COMPATIBILITE_ID,compatibilite.getId());
	 	
	 	return getBdd().update(GtfConstantes.TABLE_COMPATIBILITE, values, GtfConstantes.COL_COMPATIBILITE_ID + " = " +compatibilite.getId(), null);  
	 
	
	 
	}

	@Override
	public long remove(long compatibiliteid) {
		// TODO Auto-generated method stub
		return getBdd().delete(GtfConstantes.TABLE_COMPATIBILITE, GtfConstantes.COL_COMPATIBILITE_ID + " = " +compatibiliteid, null);  
		 
		
		 
	}

	@Override
	public List<Compatibilite> findAll() {
		// TODO Auto-generated method stub
		Cursor c = getBdd().query(GtfConstantes.TABLE_COMPATIBILITE, GtfConstantes.COLS_COMPATIBILITE, null, null, null, null, GtfConstantes.COL_COMPATIBILITE_DATE+" DESC");
		return   cursorToCompatibilites(c);
		
	}

	@Override
	public List<Compatibilite> findByElle(String elle) {
		// TODO Auto-generated method stub
		Cursor c = getBdd().query(GtfConstantes.TABLE_COMPATIBILITE, GtfConstantes.COLS_COMPATIBILITE,GtfConstantes.COL_COMPATIBILITE_ELLE+"='"+elle+"'" , null, null, null, GtfConstantes.COL_COMPATIBILITE_ELLE+" ASC");
		return   cursorToCompatibilites(c);
	
	}

	@Override
	public List<Compatibilite> findByLui(String lui) {
		// TODO Auto-generated method stub
		Cursor c = getBdd().query(GtfConstantes.TABLE_COMPATIBILITE, GtfConstantes.COLS_COMPATIBILITE,GtfConstantes.COL_COMPATIBILITE_LUI+"='"+lui+"'" , null, null, null, GtfConstantes.COL_COMPATIBILITE_LUI+" ASC");
		return   cursorToCompatibilites(c);
	
	}

	@Override
	public Compatibilite findByLuiEtElle(String lui, String elle) {
		// TODO Auto-generated method stub
		Cursor c = getBdd().query(GtfConstantes.TABLE_COMPATIBILITE, GtfConstantes.COLS_COMPATIBILITE,GtfConstantes.COL_COMPATIBILITE_ELLE+"='"+elle+"' AND "+GtfConstantes.COL_COMPATIBILITE_LUI+"='"+lui+"'", null, null, null, null);
		List<Compatibilite> l = cursorToCompatibilites(c);
		if(null!=l&&l.size()>0)return l.get(0);
		return null;
	
	}

	@Override
	public Compatibilite find(long compatibiliteid) {
		// TODO Auto-generated method stub
		Cursor c = getBdd().query(GtfConstantes.TABLE_COMPATIBILITE, GtfConstantes.COLS_COMPATIBILITE,GtfConstantes.COL_COMPATIBILITE_ID+"="+compatibiliteid, null, null, null, null);
		List<Compatibilite> l = cursorToCompatibilites(c);
		if(null!=l&&l.size()>0)return l.get(0);
		return null;
	
	}
}
